<?php
session_start();
include('../config/config.php');
header("content-type:text/html; charset=utf-8");
#error_reporting(0);
//***********
//说明：用户批量添加工具
//使用方法：1、下载本文件，放在svnmaia安装目录的下的extension/目录
//2、编辑extension目录的addon.ini(请自己创建) ：
//3、添加如下行：
//       [批量添加用户]
//       url='./batchuser.php'
//4、支持在批量注册中输入中文名，格式：username=真实姓名
//**********
?>
<!--
Author:lixuejiang
Site:http://www.scmbbs.com
Date:2009-02-19
-->
<html>
<head>
  <title>svn用户批量添加</title>
</head>
<style type='text/css'>
h1{text-align:center;}
p{margin-left:40px}
div{margin:15px}
fieldset{border:2px solid #A4CDF2;padding:20px;background:#FFFFFF;width:60%}
 legend{color:#1E7ACE;padding:3px 20px;border:2px solid #A4CDF2;background:#FFFFFF;}
 a{color:green;text-decoration:underline;}
.m{color:red;font-size:12px}
td{height:35px;}
.lb{width:122px;text-align:right;}
</style>
<body>
<?php
function safe($str)
{
       $str=htmlspecialchars($str,ENT_QUOTES);	
	return "'".mysql_real_escape_string($str)."'";
}
if($_SESSION['role'] != 'admin')
{
	echo "<h4>Only superAdmin can use this function!</h4>";
	exit;
}
include('../../../config.inc');
include('../include/dbconnect.php');
if(isset($_POST['batchinput']))//add user
{			
	include('../include/basefunction.php');
	$usrArray=preg_split('/[;, \n\r]/',$_POST['batchinput']);
	if(isset($_POST['randompwd']))
	{
		$t_pwd=rand().rand();
		$passwd=cryptMD5Pass($t_pwd);
	}else
	{
		$t_pwd=$_POST['passwd'];
		if(!empty($t_pwd))$passwd=cryptMD5Pass($t_pwd);
	}
	$u_exists='';
	foreach($usrArray as $i=>$u)
	{
		if(empty($u))continue;
		$u=str_replace('\\\'','',$u);
		list($u,$fn)=explode('=',$u);
		if(!checkUserGroup($u))
		{
			echo "<br>$u 用户名非法！";
			continue;
		}
		$u_raw=$u;
		$u=safe($u);
		$fn=safe($fn);
	  	$expire=date("Y-m-d" , strtotime("+$user_t day"));
	  	$query = "insert into svnauth_user (user_name,full_name,password,supervisor,expire) values ($u,$fn,'$passwd',0,'$expire')";
	  	mysql_query($query);
	  	$t_error=mysql_error();
	  	if(!empty($t_error)){
		  $u_exists .= " $u ";
	  	}else
	  	{
			$u_raw=escapeshellarg($u_raw); 
			$t_pwd=escapeshellarg($t_pwd);
			exec($htpasswd.' -m -b '. $passwdfile . ' '.$u_raw.' '.$t_pwd);
	  	}	
	  
	}	
	if(!empty($u_exists))echo "$u_exists 用户已存在！";
}
?>
  <p>
  <noscript><strong>您的浏览器不支持script脚本。<br>用户注册功能将不能正常使用！<br></noscript>
  	<form name='regform' action='' method=post onsubmit="return fCheck()">
	<fieldset>
  	 <legend>批量注册subversion用户</legend>
   	<table>
	<tr>
<td colspan=2>
	<textarea id='batchinput' name='batchinput' rows=13 cols=54 onfocus="cleartip()">提示：多用户名之间请用分号';'或','或空格' '进行分割或者每行一个用户。当需要给用户带上真实姓名信息时，则这样写用户名：username=真实姓名</textarea>
</td>
 	</tr>
	<tr>
		<td  class='lb'></td>
		<td ><input type="checkbox" name="randompwd" size="20" id="randompwd" value='true' onclick='hidepwd()'><label for='randompwd'>随机密码</label></td>
 	</tr>
 
	<tr id='pwd0'>
		<td  class='lb'>默认初始密码：</td>
		<td ><input type="password" name="passwd" size="20"> * </td>
	</tr>
	<tr id='pwd1'>
		<td  class='lb'>密码确认：</td>
		<td><input type="password" name="passwd0" size="20"> *</td>
	</tr>
 	
 </table>
	<table border="0" width="84%" id="table2">
	<tr>
		<td width="104"><input type=submit value="提交" onclick="return tCheck()"></td>
		<td><input type=reset value="取消"></td>
	
		</tr>
	</table>
</fieldset>
	</form>

</body>
</html>
<script language="javascript">
	<!--
var clned=0;	
function cleartip()
{
	if (clned != 0)return 0;
	clned=1;
	document.getElementById('batchinput').value='';
	document.getElementById('batchinput').style.background="yellow"

}

function hidepwd()
{
	if(document.getElementById('pwd0').style.display=="none")
	{
		document.getElementById('pwd0').style.display='';
		document.getElementById('pwd1').style.display='';
	}else
	{
		document.getElementById('pwd0').style.display='none';
		document.getElementById('pwd1').style.display='none';
	}
}
function fCheck(){
 		 
 var str=document.getElementById('batchinput').value;
 if(( regform.batchinput.value =="") ||(str.search('提示')>=0))
 {
       alert("\用户名不能为空!");
       regform.batchinput.focus();
       return false;
 }
 if(regform.randompwd.checked)return true;
 if( regform.passwd.value =="") 
 {
       alert("\密码不能为空!");
       regform.passwd.focus();
       return false;
  }

   if( ! isPassword( regform.passwd.value ) )
   {
        alert("\请重新输入密码,密码由至少6个英文字母或数字组成 !"); 
        regform.passwd.select();
        regform.passwd.focus();
        return false;
   }
  if( regform.passwd0.value =="" )
  {
      alert("\请输入密码确认 !");
      regform.passwd0.select();
      regform.passwd0.focus();
      return false;
  }
  if(  regform.passwd0.value != regform.passwd.value ) {
     alert("\两次密码输入不一致 !");
     regform.passwd.focus();
     return false;
  }

  function isPassword( password )
  {
     return /^[\w\W]{6,20}$/.test( password );
  }
  return true;
}  
function tCheck()
{
	if(!fCheck())return false;
	return true;
}


	-->
</script>

